<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActiveRecord::Errors</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActiveRecord::Errors</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activerecord/lib/active_record/validations_rb.html">
                vendor/rails/activerecord/lib/active_record/validations.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="../Object.html">
                Object
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Active Record validation is reported to and from this object, which is used
by <a href="Base.html#M001430">Base#save</a> to determine whether the
object is in a valid state to be saved. See usage example in <a
href="Validations.html">Validations</a>.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001351">[]</a>&nbsp;&nbsp;
      <a href="#M001346">add</a>&nbsp;&nbsp;
      <a href="#M001348">add_on_blank</a>&nbsp;&nbsp;
      <a href="#M001347">add_on_empty</a>&nbsp;&nbsp;
      <a href="#M001345">add_to_base</a>&nbsp;&nbsp;
      <a href="#M001357">clear</a>&nbsp;&nbsp;
      <a href="#M001359">count</a>&nbsp;&nbsp;
      <a href="#M001353">each</a>&nbsp;&nbsp;
      <a href="#M001354">each_full</a>&nbsp;&nbsp;
      <a href="#M001356">empty?</a>&nbsp;&nbsp;
      <a href="#M001355">full_messages</a>&nbsp;&nbsp;
      <a href="#M001349">invalid?</a>&nbsp;&nbsp;
      <a href="#M001360">length</a>&nbsp;&nbsp;
      <a href="#M001350">on</a>&nbsp;&nbsp;
      <a href="#M001352">on_base</a>&nbsp;&nbsp;
      <a href="#M001358">size</a>&nbsp;&nbsp;
      <a href="#M001361">to_xml</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../Enumerable.html">Enumerable</a></span>
      </div>
    </div>

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001351" class="method-detail">
        <a name="M001351"></a>

        <div class="method-heading">
          <span class="method-name">[]</span><span class="method-args">(attribute)</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Errors.html#M001350">on</a>
</p>
        </div>
      </div>

      <div id="method-M001346" class="method-detail">
        <a name="M001346"></a>

        <div class="method-heading">
          <a href="#M001346" class="method-signature">
          <span class="method-name">add</span><span class="method-args">(attribute, msg = @@default_error_messages[:invalid])</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Adds an error message (<tt>msg</tt>) to the <tt>attribute</tt>, which will
be returned <a href="Errors.html#M001350">on</a> a call to <tt><a
href="Errors.html#M001350">on</a>(attribute)</tt> for the same attribute
and ensure that this error object returns false when asked if <tt><a
href="Errors.html#M001356">empty?</a></tt>. More than one error can be
added to the same <tt>attribute</tt> in which case an array will be
returned <a href="Errors.html#M001350">on</a> a call to <tt><a
href="Errors.html#M001350">on</a>(attribute)</tt>. If no <tt>msg</tt> is
supplied, &quot;invalid&quot; is assumed.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001346-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001346-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 64</span>
64:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add</span>(<span class="ruby-identifier">attribute</span>, <span class="ruby-identifier">msg</span> = <span class="ruby-ivar">@@default_error_messages</span>[<span class="ruby-identifier">:invalid</span>])
65:       <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>] = [] <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>].<span class="ruby-identifier">nil?</span>
66:       <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>] <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">msg</span>
67:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001348" class="method-detail">
        <a name="M001348"></a>

        <div class="method-heading">
          <a href="#M001348" class="method-signature">
          <span class="method-name">add_on_blank</span><span class="method-args">(attributes, msg = @@default_error_messages[:blank])</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Will <a href="Errors.html#M001346">add</a> an error message to <a
href="Errors.html#M001353">each</a> of the attributes in
<tt>attributes</tt> that is blank (using Object#blank?).
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001348-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001348-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 79</span>
79:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add_on_blank</span>(<span class="ruby-identifier">attributes</span>, <span class="ruby-identifier">msg</span> = <span class="ruby-ivar">@@default_error_messages</span>[<span class="ruby-identifier">:blank</span>])
80:       <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">attr</span> <span class="ruby-keyword kw">in</span> [<span class="ruby-identifier">attributes</span>].<span class="ruby-identifier">flatten</span>
81:         <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">:</span> <span class="ruby-ivar">@base</span>[<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>]
82:         <span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">msg</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">blank?</span>
83:       <span class="ruby-keyword kw">end</span>
84:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001347" class="method-detail">
        <a name="M001347"></a>

        <div class="method-heading">
          <a href="#M001347" class="method-signature">
          <span class="method-name">add_on_empty</span><span class="method-args">(attributes, msg = @@default_error_messages[:empty])</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Will <a href="Errors.html#M001346">add</a> an error message to <a
href="Errors.html#M001353">each</a> of the attributes in
<tt>attributes</tt> that is empty.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001347-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001347-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 70</span>
70:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add_on_empty</span>(<span class="ruby-identifier">attributes</span>, <span class="ruby-identifier">msg</span> = <span class="ruby-ivar">@@default_error_messages</span>[<span class="ruby-identifier">:empty</span>])
71:       <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">attr</span> <span class="ruby-keyword kw">in</span> [<span class="ruby-identifier">attributes</span>].<span class="ruby-identifier">flatten</span>
72:         <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">:</span> <span class="ruby-ivar">@base</span>[<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>]
73:         <span class="ruby-identifier">is_empty</span> = <span class="ruby-identifier">value</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value str">&quot;empty?&quot;</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">false</span>
74:         <span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">msg</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-operator">!</span><span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">is_empty</span>
75:       <span class="ruby-keyword kw">end</span>
76:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001345" class="method-detail">
        <a name="M001345"></a>

        <div class="method-heading">
          <a href="#M001345" class="method-signature">
          <span class="method-name">add_to_base</span><span class="method-args">(msg)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Adds an error to the base object instead of any particular attribute. This
is used to report errors that don&#8216;t tie to any specific attribute,
but rather to the object as a whole. These error messages don&#8216;t get
prepended with any field name when iterating with <a
href="Errors.html#M001354">each_full</a>, so they should be complete
sentences.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001345-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001345-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 56</span>
56:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add_to_base</span>(<span class="ruby-identifier">msg</span>)
57:       <span class="ruby-identifier">add</span>(<span class="ruby-identifier">:base</span>, <span class="ruby-identifier">msg</span>)
58:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001357" class="method-detail">
        <a name="M001357"></a>

        <div class="method-heading">
          <a href="#M001357" class="method-signature">
          <span class="method-name">clear</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Removes all errors that have been added.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001357-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001357-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 192</span>
192:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">clear</span>
193:       <span class="ruby-ivar">@errors</span> = {}
194:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001359" class="method-detail">
        <a name="M001359"></a>

        <div class="method-heading">
          <span class="method-name">count</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Errors.html#M001358">size</a>
</p>
        </div>
      </div>

      <div id="method-M001353" class="method-detail">
        <a name="M001353"></a>

        <div class="method-heading">
          <a href="#M001353" class="method-signature">
          <span class="method-name">each</span><span class="method-args">() {|attr, msg| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Yields <a href="Errors.html#M001353">each</a> attribute and associated
message per error added.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.each{|attr,msg| puts &quot;#{attr} - #{msg}&quot; } # =&gt;
    name - is too short (minimum is 5 characters)
    name - can't be blank
    address - can't be blank
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001353-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001353-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 138</span>
138:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each</span>
139:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">each_key</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span> <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attr</span>].<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">msg</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">attr</span>, <span class="ruby-identifier">msg</span> } }
140:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001354" class="method-detail">
        <a name="M001354"></a>

        <div class="method-heading">
          <a href="#M001354" class="method-signature">
          <span class="method-name">each_full</span><span class="method-args">() {|msg| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Yields <a href="Errors.html#M001353">each</a> full error message added. So
Person.errors.<a href="Errors.html#M001346">add</a>(&quot;first_name&quot;,
&quot;can&#8216;t be empty&quot;) will be returned through iteration as
&quot;First name can&#8216;t be empty&quot;.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.each_full{|msg| puts msg } # =&gt;
    Name is too short (minimum is 5 characters)
    Name can't be blank
    Address can't be blank
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001354-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001354-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 155</span>
155:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each_full</span>
156:       <span class="ruby-identifier">full_messages</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">msg</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">msg</span> }
157:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001356" class="method-detail">
        <a name="M001356"></a>

        <div class="method-heading">
          <a href="#M001356" class="method-signature">
          <span class="method-name">empty?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns true if no errors have been added.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001356-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001356-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 187</span>
187:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">empty?</span>
188:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">empty?</span>
189:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001355" class="method-detail">
        <a name="M001355"></a>

        <div class="method-heading">
          <a href="#M001355" class="method-signature">
          <span class="method-name">full_messages</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns all the full error messages in an array.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.full_messages # =&gt;
    [&quot;Name is too short (minimum is 5 characters)&quot;, &quot;Name can't be blank&quot;, &quot;Address can't be blank&quot;]
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001355-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001355-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 169</span>
169:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">full_messages</span>
170:       <span class="ruby-identifier">full_messages</span> = []
171: 
172:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span>
173:         <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attr</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">msg</span><span class="ruby-operator">|</span>
174:           <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">nil?</span>
175: 
176:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attr</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;base&quot;</span>
177:             <span class="ruby-identifier">full_messages</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">msg</span>
178:           <span class="ruby-keyword kw">else</span>
179:             <span class="ruby-identifier">full_messages</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">human_attribute_name</span>(<span class="ruby-identifier">attr</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">&quot; &quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">msg</span>
180:           <span class="ruby-keyword kw">end</span>
181:         <span class="ruby-keyword kw">end</span>
182:       <span class="ruby-keyword kw">end</span>
183:       <span class="ruby-identifier">full_messages</span>
184:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001349" class="method-detail">
        <a name="M001349"></a>

        <div class="method-heading">
          <a href="#M001349" class="method-signature">
          <span class="method-name">invalid?</span><span class="method-args">(attribute)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns true if the specified <tt>attribute</tt> has errors associated with
it.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.invalid?(:name)      # =&gt; true
  company.errors.invalid?(:address)   # =&gt; false
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001349-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001349-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 96</span>
96:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">invalid?</span>(<span class="ruby-identifier">attribute</span>)
97:       <span class="ruby-operator">!</span><span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>].<span class="ruby-identifier">nil?</span>
98:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001360" class="method-detail">
        <a name="M001360"></a>

        <div class="method-heading">
          <span class="method-name">length</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Errors.html#M001358">size</a>
</p>
        </div>
      </div>

      <div id="method-M001350" class="method-detail">
        <a name="M001350"></a>

        <div class="method-heading">
          <a href="#M001350" class="method-signature">
          <span class="method-name">on</span><span class="method-args">(attribute)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns nil, if no errors are associated with the specified
<tt>attribute</tt>. Returns the error message, if one error is associated
with the specified <tt>attribute</tt>. Returns an array of error messages,
if more than one error is associated with the specified <tt>attribute</tt>.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.on(:name)      # =&gt; [&quot;is too short (minimum is 5 characters)&quot;, &quot;can't be blank&quot;]
  company.errors.on(:email)     # =&gt; &quot;can't be blank&quot;
  company.errors.on(:address)   # =&gt; nil
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001350-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001350-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 113</span>
113:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">on</span>(<span class="ruby-identifier">attribute</span>)
114:       <span class="ruby-identifier">errors</span> = <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>]
115:       <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">nil?</span>
116:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">errors</span>
117:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001352" class="method-detail">
        <a name="M001352"></a>

        <div class="method-heading">
          <a href="#M001352" class="method-signature">
          <span class="method-name">on_base</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns errors assigned to the base object through <a
href="Errors.html#M001345">add_to_base</a> according to the normal rules of
<a href="Errors.html#M001350">on</a>(attribute).
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001352-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001352-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 122</span>
122:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">on_base</span>
123:       <span class="ruby-identifier">on</span>(<span class="ruby-identifier">:base</span>)
124:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001358" class="method-detail">
        <a name="M001358"></a>

        <div class="method-heading">
          <a href="#M001358" class="method-signature">
          <span class="method-name">size</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the total number of errors added. Two errors added to the same
attribute will be counted as such.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001358-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001358-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 197</span>
197:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">size</span>
198:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-value">0</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">error_count</span>, <span class="ruby-identifier">attribute</span><span class="ruby-operator">|</span> <span class="ruby-identifier">error_count</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">size</span> }
199:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001361" class="method-detail">
        <a name="M001361"></a>

        <div class="method-heading">
          <a href="#M001361" class="method-signature">
          <span class="method-name">to_xml</span><span class="method-args">(options={})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Return an XML representation of this error object.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.to_xml # =&gt;
    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;errors&gt;
      &lt;error&gt;Name is too short (minimum is 5 characters)&lt;/error&gt;
      &lt;error&gt;Name can't be blank&lt;/error&gt;
      &lt;error&gt;Address can't be blank&lt;/error&gt;
    &lt;/errors&gt;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001361-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001361-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 219</span>
219:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_xml</span>(<span class="ruby-identifier">options</span>={})
220:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root</span>] <span class="ruby-operator">||=</span> <span class="ruby-value str">&quot;errors&quot;</span>
221:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>] <span class="ruby-operator">||=</span> <span class="ruby-value">2</span>
222:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>] <span class="ruby-operator">||=</span> <span class="ruby-constant">Builder</span><span class="ruby-operator">::</span><span class="ruby-constant">XmlMarkup</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">:indent</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>])
223: 
224:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>].<span class="ruby-identifier">instruct!</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:skip_instruct</span>)
225:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>].<span class="ruby-identifier">errors</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span>
226:         <span class="ruby-identifier">full_messages</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">msg</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">error</span>(<span class="ruby-identifier">msg</span>) }
227:       <span class="ruby-keyword kw">end</span>
228:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>